<?php
/*
+--------------------------------------------------------------------------
|   phpBIZ v3.0  full version
|   ========================================
|   by taft@wjl.cn yejun@wjl.cn
|   http://www.phpbiz.cn http://www.wjl.cn
|   all rights reserved
+---------------------------------------------------------------------------
|
|   > 最后修改日期：2005-4-30 2006-2-8
|
+--------------------------------------------------------------------------
*/
!class_exists('Debug') && exit('Forbidden');
$gd = new GoodsDetail;

class GoodsDetail
{	
	var $id;
	var $pic_info;
	function GoodsDetail()
	{
		global $DB,$BIZ,$INCOME;
		$this->id = intval( $INCOME['GOODS_ID'] );
		$BIZ -> assign('code',$INCOME['code']);
       $BIZ -> nav_link[] = array("查看商品","");
	   	//--------------------------------------
	   	//check if exsit
	   	//--------------------------------------
        $DB -> db_query( "SELECT count(*) as ct FROM biz_goods WHERE goods_id='".$this->id."'" );
		$row = $DB -> db_fetch_row();
		if( $row['ct'] !=1 ) $BIZ -> error( "查询错误","?act=Lst&code=g" );
	   	//--------------------------------------
	   
		switch($INCOME['code'])
		{
			case '00':
				$this -> list_attpic();
				//break;
			case 'addm':
				$this -> add_comment();
				//break;
			default:
				$this -> show_general();
		}
		$BIZ -> output("GoodsDetail.tpl");
	}
	
	function show_general()
	{
		global $DB,$BIZ,$INCOME;
		//--------------------------------------
		//retrive message
		//--------------------------------------
		$DB -> db_query( "SELECT * FROM biz_message WHERE message_owner_goods_id ='".$this->id."' ORDER BY `message_write_date` DESC ");
	   	while( $r_m = $DB -> db_fetch_row() )
		{
			$tm = date( "m/d H:i:s",$r_m['message_write_date'] );
		   	$m[]=array( $r_m['message_writer_title'],$r_m['message_content'],$tm,$r_m['message_reply_content'] );
		}

		$BIZ -> assign("message",$m);	   
	   	//----------------------------------------
	   	
	   $DB -> db_query( "UPDATE biz_goods SET goods_visit_count = goods_visit_count+1 WHERE goods_id='".$this->id."'" );
	   $DB  -> db_query( "SELECT g.*, p.pic_name,s.*,b.seller_logaccount,b.seller_qq_number,d.repute FROM biz_goods g LEFT JOIN biz_pic p ON p.pic_id = g.goods_prime_picture_id LEFT JOIN biz_shop s ON s.shop_id = g.shop_id LEFT JOIN biz_seller b ON b.seller_id = g.seller_id LEFT JOIN biz_seller_data d ON d.seller_id = b.seller_id WHERE goods_id='".$this->id."' AND g.goods_mark_sell='0' ");
	   $g = $DB -> db_fetch_row();
	   $repute_array = array();
	   $BIZ -> assign("repute_ct",array_pad($repute_array,repute_compute($g['repute']),0) );
           $g['shop_describle'] = $g['shop_describle'];
	       $g['goods_describle'] = nl2br($g['goods_describle']); //not determine yet
           $g['goods_store_date'] = date("Y-m-d",$g['goods_store_date']); 
		   $g['pic_name']=$BIZ -> make_full_path($g['pic_name']);
	   $BIZ -> assign("glist",$g);
	   
	   //--------------------------------------------
	   //display attached pictures' management hint
	   //--------------------------------------------
	   $DB -> db_query("SELECT count(*) FROM biz_pic_attach where pic_goods_id=$this->id");
	   $crs = $DB -> db_fetch_row();
	   $pic_count = $crs[0];
	   $hint = $pic_count ? "该商品有<font color=red>".$pic_count."</font>张副图，<a href=index.php?act=gd&code=00&GOODS_ID=$this->id><font color=red>点击查看</font></a>" : "该商品没有任何副图，店主可以在登录后<a href=index.php?act=attpic&GOODS_ID=$this->id>上传</a>";
	   $BIZ -> assign("hint", $hint);
	   //---------------------------------------------		
	}
	
	function list_attpic()
	{	//00
		global $DB,$BIZ;
		$DB -> db_query("SELECT `pic_name` FROM `biz_pic_attach` WHERE `pic_goods_id`='{$this->id}'");
		while($p_re=$DB -> db_fetch_row())
		{	
			$pics[]=array($BIZ->get_thumb($p_re['pic_name']),$BIZ->make_full_path($p_re['pic_name']));
		}
		$BIZ -> assign('pic_info',$pics);
	}
	
	function add_comment()
	{	//01
		global $DB,$BIZ,$INCOME;
		$DB -> db_query("SELECT count(*) as ct FROM biz_message WHERE message_content='".$INCOME['content']."'");
	   	$num = $DB -> db_fetch_row();
	   	if( $INCOME[code] == 'addm' && $INCOME['content'] && !$num['ct'] )
	   	{
			session_start();
		  	if(md5($INCOME['acode'])!=$_SESSION['authcode'])
				$BIZ -> error("验证码错误！");
		  	else
				unset($_SESSION['authcode']);

		$tm =time();
	    $ty =MESSAGE_GOODS;
	    $query= "INSERT INTO `biz_message` ( `message_content`, `message_writer_title`,`message_write_date`,`message_type` ,`message_owner_goods_id`,`ip`) VALUES ('{$INCOME[content]}','{$INCOME[writer]}','{$tm}','{$ty}','{$this->id}','{$INCOME[IP_ADDRESS]}')";

	    $DB -> db_query($query);
		 // update goods reply time and double visit count
		$query = " UPDATE biz_goods SET goods_reply_date =".time().", goods_visit_count = goods_visit_count +1 WHERE goods_id='".$this->id."'";
		$DB -> db_query($query);
	   }
	}
	
}
?>